package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.ProjectService;
import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.model.ProjectEntity;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xiyong.lxy
 * @date 2021-06-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/project")
public class ProjectController2 {
    @Autowired
    private ProjectService projectService;

    @Data
    public static class Params {
        public Long id;
        public String ids;
        public String projectName;
    }

    @RestGrpcMapping("/listProject.do")
    public NTSTATUS listProject(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            List<ProjectEntity> items = projectService.listProject();
            log.info("success.");
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    @RestGrpcMapping("/getProject.do")
    public NTSTATUS getProject(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            ProjectEntity item = projectService.getProject(aa.id);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    @RestGrpcMapping("/addProject.do")
    public NTSTATUS addProject(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            ProjectEntity aa = params.driverObject(ProjectEntity.class);
            Object res = projectService.addProject(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/deleteProject.do")
    public NTSTATUS deleteProject(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Params aa = params.driverObject(Params.class);
            Object res = projectService.deleteProject(aa.ids);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
